package cn.agiledata.bank.transaction.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.agiledata.bank.common.dao.HqlPageListTemplate;
import cn.agiledata.bank.common.util.DateUtil;
import cn.agiledata.bank.transaction.logic.RefundQueryBean;

public class RefundQueryPageList extends HqlPageListTemplate {
    private RefundQueryBean bean;

    private List refundTransStatusList;

    public RefundQueryPageList(RefundQueryBean bean, List refundTransStatusList) {
        this.bean = bean;
        this.refundTransStatusList = refundTransStatusList;
    }

    public String getHql() {
        String hql = "from Transaction t where t.debit.account=:account ";
        if (this.bean.getSubAccountId() != null) {
            hql += "and t.debit.id=:subAccountId ";
        }
        if (this.bean.getTransStatus() != null
                && this.bean.getTransStatus().length() > 0) {
            hql += "and t.transStatus = :transStatus ";
        } else {
            hql += "and t.transStatus in (:transStatusList) ";
        }
        if (this.bean.getBeginDate() != null) {
            hql += "and t.beginDate>=:beginDate ";
        }
        if (this.bean.getEndDate() != null) {
            hql += "and t.beginDate<:endDate ";
        }
        if (this.bean.getPeerAccount() != null) {
            hql += "and t.credit.account=:peerAccount ";
        }
        if (this.bean.getTransId() != null
                && this.bean.getTransId().length() > 0) {
            hql += "and t.transId like :transId ";
        }
        if (this.bean.getMinAmount() != null) {
            hql += "and t.amount>=:minAmount ";
        }
        if (this.bean.getMaxAmount() != null) {
            hql += "and t.amount<=:maxAmount ";
        }
        hql += "order by t.beginDate desc";
        return hql;
    }

    public String getPageRecord() {
        return "20";
    }

    public Map getParams() {
        Map params = new HashMap();
        params.put("account", this.bean.getAccount());
        if (this.bean.getSubAccountId() != null) {
            params.put("subAccountId", this.bean.getSubAccountId());
        }
        if (this.bean.getTransStatus() != null
                && this.bean.getTransStatus().length() > 0) {
            params.put("transStatus", this.bean.getTransStatus());
        } else {
            params.put("transStatusList", this.refundTransStatusList);
        }
        if (this.bean.getBeginDate() != null) {
            params.put("beginDate", DateUtil.earliestOfDate(this.bean.getBeginDate()));
        }
        Date endDate = this.bean.getEndDate();
        if (endDate != null) {
            endDate = DateUtil.latestOfDate(endDate);
            params.put("endDate", endDate);
        }
        if (this.bean.getPeerAccount() != null) {
            params.put("peerAccount", this.bean.getPeerAccount());
        }
        if (this.bean.getTransId() != null
                && this.bean.getTransId().length() > 0) {
            params.put("transId", "%" + this.bean.getTransId() + "%");
        }
        if (this.bean.getMinAmount() != null) {
            params.put("minAmount", this.bean.getMinAmount());
        }
        if (this.bean.getMaxAmount() != null) {
            params.put("maxAmount", this.bean.getMaxAmount());
        }
        return params;
    }

    public String getStartPage() {
        String startPage = this.bean.getStartPage();
        if (startPage == null || startPage.length() == 0) {
            startPage = "1";
        }
        return startPage;
    }

}
